/*
 * Copyright (c) 2008 Ricardo Rocha, rocha.porto@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.gliding.kumulus.client.model;

import java.util.Date;
import java.util.HashMap;

/**
 * Definition of an ActivityDaySummary object.
 * 
 * It represents the full activity of a single day, including the presence of members
 * and the different roles they will have during the day.
 * 
 * The activity is split in two halfs: morning and afternoon.
 * 
 * @author Ricardo Rocha <rocha.porto@gmail.com>
 * @version $Rev:: $
 */
public class ActivityDaySummary {
	
	/**
	 * The object containing the date corresponding to this activity summary.
	 */
	private Date date;
	
	/**
	 * The morning activity summary entries.
	 * 
	 * The Map object is keys on the possible member roles, with each value being
	 * and array of Member objects.
	 */
	private HashMap morningEntries = new HashMap();
	
	/**
	 * The morning activity summary entries.
	 * 
	 * The Map object is keys on the possible member roles, with each value being
	 * and array of Member objects.
	 */
	private HashMap afternoonEntries = new HashMap();
	
	/**
	 * Static integer representing the first half of the day.
	 */
	public static final int MORNING = 0;
	
	/**
	 * Static integer representing the second half of the day.
	 */
	public static final int AFTERNOON = 1;
	
	/**
	 * Class constructor.
	 * 
	 * @param date The day of this object
	 */
	public ActivityDaySummary(Date date) {
		this.date = date;
	}
	
	/**
	 * Updates the activity entries of the given role for this activity summary.
	 * 
	 * @param role The role to associate the given members to
	 * @param partOfDay The part of the day to update (ActivityDaySummary.MORNING/AFTERNOON)
	 * @param members The array containing the members which are to be set as having the given role in the
	 * given part of the day
	 */
	public void updateActivity(String role, int partOfDay, Member[] members) {
		if (partOfDay == ActivityDaySummary.MORNING) {
			this.morningEntries.put(role, members);
		} else if (partOfDay == ActivityDaySummary.AFTERNOON) {
			this.afternoonEntries.put(role, members);
		} else {
			throw new IllegalArgumentException("'" + partOfDay + "' is not a valid identifier of a part of day");
		}
	}
	
	/**
	 * Returns the activity entries corresponding to the given role / part of the day.
	 * 
	 * @param role The role for which to return the activity entries
	 * @param partOfDay The part of the day for which to return the activity entries
	 * @return A list of members having the requested role in the requested part of the day
	 */
	public Member[] getActivityEntries(String role, int partOfDay) {
		Member[] tmpEntries = null;
		if (partOfDay == ActivityDaySummary.MORNING) {
			tmpEntries = (Member[])morningEntries.get(role);
		} else if (partOfDay == ActivityDaySummary.AFTERNOON) {
			tmpEntries = (Member[])afternoonEntries.get(role);
		}
		// Make sure to return an empty object instead of null to avoid null pointer exceptions
		if (tmpEntries == null) {
			return new Member[0];
		}
		return tmpEntries;
	}
	
	/**
	 * Returns the date of this ActivityDaySummary object.
	 * 
	 * @return The date of this object
	 */
	public Date getDate() {
		return this.date;
	}
	
	/**
	 * Removes one activity entry from this activity day summary.
	 * 
	 * @param role The role to which the entry belongs
	 * @param partOfDay The part of the day the entry is located at
	 * @param memberID The ID of the member to be removed
	 */
	public void removeActivityEntry(String role, int partOfDay, int memberID) {
		HashMap entries = null;
		if (partOfDay == ActivityDaySummary.MORNING) {
			entries = morningEntries;
		} else {
			entries = afternoonEntries;
		}
		
		Member[] members = (Member[])entries.get(role);
		Member[] newMembers = new Member[members.length-1];
		int j = 0;
		for (int i=0; i<members.length; i++) {
			if (members[i].id != memberID) {
				newMembers[j] = members[i];
				j++;
			}
		}
		
		entries.put(role, newMembers);		
	}
	
}